﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace DBManager.Admin
{
    public class EmployeeDAL
    {
        public static string EditStatusEmployee(string ID, string status)
        {
            string kq = "0";
            string sql = @"update Employee set status=" + comm.to_sqlnumber(status) + " where ID=" + comm.to_sqlnumber(ID);
            try
            {
                DBUtil.Execute(sql);
                kq = "1";
            }
            catch (Exception)
            {
                kq = "0";
            }
            return kq;
        }
        public static DataTable GetInfomationByID(string ID)
        {
            //thong tin chi tiet
            string sql = "select * from Employee where ID=" + comm.to_sqlnumber(ID);
            return DBUtil.SelectTable(sql);
        }
        public static DataTable GetAllEmployee(string status)
        {
            //thong tin 
            string sql = "select ID,Name from Employee where Status=" + comm.to_sqlnumber(status) + " order by Name asc";
            return DBUtil.SelectTable(sql);
        }
        public static DataTable GetAllEmployeeAdmin(string search, string checkStatus)
        {
            //thong tin 
            string dkSearch = " and Name like N'%" + search + "%' ";
            if (search == "")
            {
                dkSearch = "";
            }
            //status
            string dkCheckStatus = " and status= " + comm.to_sqlnumber(checkStatus) + " ";
            string dkStatus = ",case Status when 1 then N'Đang làm việc' when 0 then N'Khóa lại' end as TrangThai";
            string dkSex = ",case Sex when 1 then N'Nam' when 0 then N'Nữ' end as GioiTinh";
            string sql = "select *" + dkStatus + dkSex + " from Employee where 1=1 " + dkSearch + dkCheckStatus + " order by Name asc";
            return DBUtil.SelectTable(sql);
        }

        public static int AddEmployee(string Name, string Email, string Tel, string Address, string Birthday, string Descriptions, string InfomationOther, string Status, string Sex)
        {
            //sua cong ty
            int kq = 0;
            try
            {
                string sql = @"INSERT INTO Employee(Name,Email,Tel,Address,Birthday,Descriptions,InfomationOther,Status,Sex) 
                                VALUES 
                               (" + comm.to_sqltext(Name) + @"
                               ," + comm.to_sqltext(Email) + @"
                               ," + comm.to_sqltext(Tel) + @"
                               ," + comm.to_sqltext(Address) + @"
                               ," + comm.to_sqltext(Birthday) + @"
                               ," + comm.to_sqltext(Descriptions) + @"
                               ," + comm.to_sqltext(InfomationOther) + @"
                               ," + comm.to_sqlnumber(Status) + @"
                               ," + comm.to_sqlnumber(Sex) + ")";
                DBUtil.Execute(sql);
                kq = 1;
            }
            catch (Exception)
            {
                kq = 0;
            }
            return kq;
        }
        public static int EditEmployee(string Name, string Email, string Tel, string Address, string Birthday, string Descriptions, string InfomationOther, string Status, string Sex, string ID)
        {
            //sua cong ty
            int kq = 0;
            try
            {
                string sql = @"UPDATE Employee 
                              SET Name = " + comm.to_sqltext(Name) + @"
                              ,Email = " + comm.to_sqltext(Email) + @"
                              ,Tel = " + comm.to_sqltext(Tel) + @"
                              ,Address = " + comm.to_sqltext(Address) + @"
                              ,Birthday = " + comm.to_sqltext(Birthday) + @"
                              ,Descriptions = " + comm.to_sqltext(Descriptions) + @"
                              ,InfomationOther = " + comm.to_sqltext(InfomationOther) + @"
                              ,Status = " + comm.to_sqlnumber(Status) + @"
                              ,Sex = " + comm.to_sqlnumber(Sex) + @"
                              WHERE ID=" + comm.to_sqlnumber(ID);
                DBUtil.Execute(sql);
                kq = 1;
            }
            catch (Exception)
            {
                kq = 0;
            }
            return kq;
        }
    }
}
